package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.aliyun.oss.OSSClient;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.OSSClientConstants;
import com.itheima.constant.RedisConstants;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.AliyunOSSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 套餐控制层
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    //远程注入套餐服务
    @Reference
    private SetmealService setmealService;

    //redis注入
    @Autowired
    private JedisPool jedisPool;

    /**
     * 据套餐Id查询检查组Ids
     * @param setmealId
     * @return
     */
    @GetMapping("/findcheckGroupIdsBySetmealId")
    public Result findcheckGroupIdsBySetmealId(Integer setmealId) {
        try {
            Integer[] checkGroupIds = setmealService.findcheckGroupIdsBySetmealId(setmealId);
            return new Result (true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result (false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 前端图片上传阿里云并回显
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        try {
            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFilename.substring(lastIndexOf - 1);
            //使用UUID随机产生文件名称，防止同名文件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;
            //阿里云oss工具类生成OSSClient
            OSSClient ossClient = AliyunOSSClientUtil.getOSSClient();
            //OSSClient上传图片方法
            AliyunOSSClientUtil.myUpload(ossClient,imgFile.getBytes(), OSSClientConstants.BACKET_NAME, fileName);

            //将上传成功的图片名保存至redis
            jedisPool.getResource().sadd(RedisConstants.SETMEAL_PIC_RESOURCES, fileName);

            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 增加套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {

            setmealService.add(setmeal, checkgroupIds);

            //增加套餐后再将图片名存入redis
            if (setmeal.getImg()!=null) {
                jedisPool.getResource().sadd(RedisConstants.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
            }
            //将更新存放到redis中
            Jedis resource = jedisPool.getResource();
            List<Setmeal> setmeal1 = setmealService.getSetmeal();
            resource.hset("Setmeal", RedisMessageConstant.SELECT_CHECKGROUP_RESOURCES, JSON.toJSON(setmeal1).toString());
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    /**
     * 套餐分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        return setmealService.findPage(currentPage, pageSize, queryString);
    }

}
